package com.gitee.openviducn.inspector.rest;

import com.gitee.openviducn.inspector.auth.AuthAction;
import com.gitee.openviducn.inspector.auth.AuthService;
import com.gitee.openviducn.inspector.cdr.process.WebhookCDRProcessorManager;
import com.gitee.openviducn.inspector.common.BizException;
import com.gitee.openviducn.inspector.common.ResponseError;
import com.gitee.openviducn.inspector.common.ResponseResult;
import com.gitee.openviducn.inspector.domain.vo.request.WebhookCDRRequest;
import com.gitee.openviducn.inspector.enums.CDREventEnum;
import com.gitee.openviducn.inspector.cdr.process.BaseWebhookCDRProcessor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @Author: https://gitee.com/wesleyOne
 * @Date: 02.29 2020
 */
@Api(tags = "webhook回调接口")
@RequestMapping("/webhook")
@RestController
public class WebHookController extends BaseController {

    @Resource
    private WebhookCDRProcessorManager webhookCDRProcessorManager;
    /**
     * 相关参考文档：<br>
     * <a href="http://openvidu_cn.gitee.io/openvidu_docs_cn/docs/reference-docs/openvidu-server-webhook/">
     *  OpenVidu Server Webhook
     * </a>
     *
     * 需要在启动OpenVidu服务时添加启动参数,例如：
     * <code>
     * java -jar \
     *     -Dopenvidu.secret=MY_SECRET \
     *     -Dopenvidu.publicurl=https://127.0.0.1:4443/  \
     *     -Dopenvidu.webhook=true \
     *     -Dopenvidu.webhook.endpoint=http://12.34.56.78:8990/webhook/ \
     *     -Dopenvidu.webhook.headers=[\"X-Token: T1BFTlZJRFVBUFA6TVlfU0VDUkVU\"] \
     * openvidu-server.jar
     * </code>
     *
     * <code>
     * docker run --rm -p 4443:4443 --name opkms -e server.port=4443 \
     *     -e openvidu.secret=MY_SECRET \
     *     -e openvidu.publicurl=https://127.0.0.1:4443/ \
     *     -e openvidu.webhook=true \
     *     -e openvidu.webhook.endpoint=http://12.34.56.78:8990/webhook/ \
     *     -e openvidu.webhook.headers="[\"X-Token: T1BFTlZJRFVBUFA6TVlfU0VDUkVU\"]" \
     * openvidu/openvidu-server-kms:2.12.0
     * </code>
     * ps：\反斜杠是换行符，windows下自行去除后执行
     *
     * 以上的openvidu.webhook.headers的X-Token配置可以通过工具类生成：
     *  1.创建一个webhook角色用户
     *  2.通过以上用户信息，执行 com.gitee.openviducn.inspector.util.JwtUtilTest#webhook()，
     *    获取token，替代<strong>X-Token: </strong>后面的字符串(注意那个空格必须有)
     *
     * <strong>另外要确保endpoint地址是openvidu-server服务可以访问的。</strong>
     *
     * @param request   OpenviduEventWebhookRequest
     * @return 状态码200
     */
    @ApiOperation("OpenVidu的事件回调接口")
    @AuthAction(roles = {AuthService.SystemRole.WEBHOOK_CDR})
    @PostMapping("/")
    public ResponseResult<Void> openviduEvent(@RequestBody WebhookCDRRequest request) throws BizException {
        String event = request.getEvent();
        CDREventEnum cdrEventEnum = CDREventEnum.getByCode(event);
        if (Objects.isNull(cdrEventEnum)) {
            throw new BizException(ResponseError.CDE_TYPE_NULL);
        }
        BaseWebhookCDRProcessor webhookCDRProcessor = webhookCDRProcessorManager.getProcessor(event);
        if (Objects.isNull(webhookCDRProcessor)) {
            throw new BizException(ResponseError.CDR_PROCESS_NULL);
        }
        webhookCDRProcessor.process(request);
        return ResponseResult.success();
    }
}
